﻿using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using SvgViewer.Core;
using SvgViewer.Entities;

namespace SvgViewer.Utilities
{
    /// <summary>
    /// 样式管理
    /// </summary>
    public class ThemeManager : NotifyPropertyBase
    {
        private readonly PaletteHelper paletteHelper;

        /// <summary>
        /// 获取样式管理实例
        /// </summary>
        public static readonly ThemeManager Instance = new();

        private ThemeManager()
        {
            paletteHelper = new PaletteHelper();
        }

        /// <summary>
        /// 获取或设置是否深色模式
        /// </summary>
        public bool IsDark {
            get => Properties.Settings.Default.IsDark;
            set {
                var theme = paletteHelper.GetTheme();
                theme.SetBaseTheme(value ? Theme.Dark : Theme.Light);
                paletteHelper.SetTheme(theme);

                Properties.Settings.Default.IsDark = value;
                Properties.Settings.Default.Save();
                OnPropertyChanged(nameof(IsDark));
            }
        }

        /// <summary>
        /// 获取或设置使用的主色调索引
        /// </summary>
        public int ColorIndex {
            get => Properties.Settings.Default.ColorIndex;
            set {
                Properties.Settings.Default.ColorIndex = value;
                Properties.Settings.Default.Save();

                var theme = paletteHelper.GetTheme();
                var color = BackgroundManager.Instance.BackgroundColors[value];
                theme.PrimaryLight = new ColorPair(color.Lighten());
                theme.PrimaryMid   = new ColorPair(color);
                theme.PrimaryDark  = new ColorPair(color.Darken());
                paletteHelper.SetTheme(theme);
            }
        }
    }
}